package com.zhongying.jf.util;

import com.chinazyjr.framework.common.config.HConfig;
import com.google.gson.internal.LinkedTreeMap;
import com.zhongying.jf.Constant;
import com.zhongying.jf.sms.liulangmao.MD5Util;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;


public class SmsChuangRuiUtils {
	/**
	 * 短信工具类(0表示发送成功,111表示发送失败)
	 * @author lijia
	 *
	 */
/**大汉三通短信平台start**/
	
	private static final String account = HConfig.GLOBAL.getProperty("account");// 用户名（必填）
	private static final String password = HConfig.GLOBAL.getProperty("password");// 密码（必填）
	public static final String sign = HConfig.GLOBAL.getProperty("sign"); // 短信签名（必填）
	public static final String subcode = HConfig.GLOBAL.getProperty("subcode"); // 子号码（可选）
	
	public static String url =HConfig.GLOBAL.getProperty("url");
	private static final Logger LOGGER = LoggerFactory.getLogger(SmsChuangRuiUtils.class);

	private static HttpClientUtil httpClientUtil = new HttpClientUtil();
	
	/**
	 * 提交普通短信
	 * 
	 * @param content
	 *            短信内容
	 * @param phone
	 *            手机号
	 * @return resultCode
	 */
	public static int sendBatchMessage(String content, String phone) {
		int code=0;
		try {
//			if (!StringUtils.isBlank(HConfig.GLOBAL.getProperty("test_people"))){
//				for (String s: HConfig.GLOBAL.getProperty("test_people").split(",")){
//					if (s.equals(phone)){
//						code = sendSms(content,phone);
//					}
//				}
//			}else {
				code = sendSms(content,phone);
//			}
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return code;
	}

	
	/**
	 * Description:创瑞短信发送<br />
	 * </p>
	 * @author peng_zhang
	 * @version 0.1 2016-3-3
	 * @param smsContent  发送内容
	 * @param phoneNum  发送号码
	 * @return int
	 * @throws Exception 
	 */
	public static int sendSms(String smsContent, String phoneNum)throws Exception{
		return sendSms(smsContent, phoneNum,"");
	}
	/**
	 * Description:创瑞短信发送<br />
	 * </p>
	 * @author peng_zhang
	 * @version 0.1 2016-3-3
	 * @param smsContent  发送内容
	 * @param phoneNum  发送号码
	 * @param sign  签名
	 * @return int
	 * @throws Exception 
	 */
	public static int sendSms(String smsContent, String phoneNum, String sign) throws Exception{
		return sendSms(smsContent, phoneNum, sign, "");
	}
	/**
	 * Description:创瑞短信发送<br />
	 * </p>
	 * @author peng_zhang
	 * @version 0.1 2016-3-3
	 * @param smsContent  发送内容
	 * @param phoneNum  发送号码
	 * @param sign   签名
	 * @return int
	 * @throws Exception 
	 */
	public static int sendSms(String smsContent, String phoneNum, String sign,String stime) throws Exception {
		System.out.println("------>发送一条短信!!");
		int sendStatus = 111;
		Map<String, Object> msgParamDto = new HashMap<String, Object>();
		msgParamDto.put("account", account);
		msgParamDto.put("password", MD5Util.MD5(password));
		msgParamDto.put("phones", phoneNum);
		msgParamDto.put("content", smsContent);
		msgParamDto.put("sign", sign);
		msgParamDto.put("subcode", subcode);
		msgParamDto.put("sendtime", stime);
		String params = JSONObject.fromObject(msgParamDto).toString();
		String outStr = null;
		HttpResponse httpResponse = httpClientUtil.doPost(url,params);
		outStr = httpClientUtil.getHttpContent(httpResponse);
		LOGGER.info("短信响应输出:"+JacksonUtil.bean2Json(outStr));
		LinkedTreeMap resultMap= (LinkedTreeMap)JSONObject.toBean(JSONObject.fromObject(outStr),LinkedTreeMap.class); //JSONAlibabaUtil.toBean(JSONAlibabaUtil.parseString(string), Map.class);
		if((resultMap.get("result")).equals("0")){
			sendStatus =0;
			LOGGER.info("提交单条普通短信响应：" + resultMap.toString());
		}

		return sendStatus;
	}
	
	public static void main(String[] args) {
		System.out.println("---->"+ SmsChuangRuiUtils.sendBatchMessage("hahaha", "18565321959"));

		String str= null;
		String str1= "";
		System.out.println(StringUtils.isNotEmpty(str1));
	}
}
